<template>
	<view class="body">
		<view class="search-box">
			<input type="text" placeholder="请输入或扫描条码" class="search-input" v-model="code" @confirm="doByInput" confirm-type="search"/>
			<view class="search-btn" @tap="doByInput">
				<image src="../../static/icons/icon_search2.png" class="btn-icon"></image>
			</view>
			<!-- /btn-group -->
		</view>
		<view class="divider">
			
		</view>	
		<!-- <view class="menu-wrapper">
			<input type="text" placeholder="请输入或扫描条码" class="input" :value="code" @confirm="doByInput" confirm-type="search"/>			
		</view>
		<view class="divider">
			
		</view> -->
		
		<v-table :columns="columns" :list="listData" :showIndex="true"></v-table>
<!-- 		<view class="menu-wrapper space">
			编码 : {{code}}
		</view>
		<view class="menu-wrapper">
			仓库编号 : {{wrhus_id}}
		</view>		
		<view class="menu-wrapper">
			货位编号 : {{invent_code}}
		</view>
		<view class="menu-wrapper">
			操作人 : {{create_who}}
		</view>		
		<view class="menu-wrapper">
			操作时间 : {{create_date}}
		</view> -->
	</view>
</template>

<script>
	import SCan from 'common/scan.js'
	import vTable from '../../components/tablewithindex.vue'
	
	export default {
		components:{
			vTable
		},
		data() {
			return {
				code:"",
				wrhus_id:"",
				invent_code:"",
				create_who:"",
				create_date:"",
				listData:[],
				columns: [
					{
						title: '编码',
						key: 'code',
						$width:'140px'
					},
					{
						title: '仓库编号',
						key: 'wrhus_id'
					},
					{
						title: '货位编号',
						key: 'inventlocdef_code'
					},
					{
						title: '操作人',
						key: 'create_who'
					},
					{
						title: '操作时间',
						key: 'create_date',
						$width:'140px'
					}
				]
			}
		}, 
		onShow: function(){  
			if(this.$config.SysConfig.wrhus_id==""|| this.$config.SysConfig.inventlocdef_code=="" || this.$config.SysConfig.wrhus_id==undefined|| this.$config.SysConfig.inventlocdef_code==undefined){
				uni.showModal({
					title:'提示',
					content:'当前仓库和货位未知,请先扫描货位码!',
					success: (res) => {
						if (res.confirm) {
							uni.navigateTo({
								url:'../wrhus/wrhus?index=offlineoperate'
							})
						}
						else{
							uni.navigateBack({
								delta:1
							})
						}
					}
				})
			} else {
				uni.setNavigationBarTitle({
					title:"离线操作 "+this.$config.SysConfig.wrhus_id+"--"+this.$config.SysConfig.inventlocdef_code
				});
				var _this=this;
				_this.$local.ScanHistoryMaxCounter((res)=>{
					var array=JSON.parse(res.data);
					if(array.length>0){
						_this.$config.SysConfig.counter_offline=array[0].counter;
					}
					// console.log(res);
					console.log(_this.$config.SysConfig.counter_offline);
				},(e)=>{
					console.log(e);
				})
			}
		    SCan.initScan(this.queryAndScan)  
		    SCan.startScan();  
		},  
		onLoad:function(){
			var _this=this;
			_this.$local.ScanHistory((res)=>{
				// console.log(res.data);
				var array=JSON.parse(res.data);
				_this.listData=array;
			},()=>{})
		},
		onHide:function(){  
		    SCan.stopScan();  
		},  
		onUnload:function(){
		    SCan.stopScan();
		}, 
		onNavigationBarButtonTap:function(e){
			const index=e.index;
			if(index===0){
				uni.showActionSheet({
					itemList:['切换货位'],
					success:function(res){
						if(res.tapIndex===0){
							uni.redirectTo({
								url:'../wrhus/wrhus?index=offlineoperate'
							})
						}
					}
				})
			}
		},
		methods: {
			doByInput(e){
				var _this=this;
				_this.queryAndScan(_this.code)			
			},
			queryAndScan(code){
				var _this=this;
				_this.$common.checkPopedom(()=>{
					_this.code=code;
					if(_this.code!=""){
						//记录扫描记录
						var date=new Date();
						this.$config.SysConfig.TODAY_SCAN.push({
							page:'离线',
							code:code,
							date:date.getHours()+':'+date.getMinutes()+":" +date.getSeconds()
						})
						
						_this.$common.formatDtTime(date,(datestr)=>{
							_this.$config.SysConfig.counter_offline+=1;
							var ScanHistoryData=[{code:''+_this.code.replace(/[\r\n]/g,"")+''
							,wrhus_id:''+_this.$config.SysConfig.wrhus_id+''
							,inventlocdef_code:''+_this.$config.SysConfig.inventlocdef_code+''
							,create_who:''+_this.$config.SysConfig.loginedUser.loginname+''
							,create_date:''+datestr+'',counter:''+_this.$config.SysConfig.counter_offline+''}]
							
							_this.$local.offlineOp(ScanHistoryData,(res)=>{
								// console.log(res)
								var array=JSON.parse(res.data);
								_this.listData=array;
							},(e)=>{
								// console.log(e);
								SCan.stopScan();
								uni.showModal({
									title:'提示',
									content:e,
									showCancel:false,
									success: (resp) => {
										if(resp.confirm){
											SCan.startScan();
										}
									}
								})
								// _this.$common.playErrBeep(1);
							})
						})					
					}
					else {
						console.log('err');
						// _this.$common.playErrBeep(1);
					}
				})
				
			}
		}
	}
</script>

<style>
.body{
	border-top: solid 1px #DBDBDA;
	padding: 0upx;
}
.input{
	width: 100%;
	height: 85upx;
	padding-right: 80upx;
	background: url('../../static/icons/qrcode.png') no-repeat scroll right center transparent;
}
.menu-wrapper{
	flex-direction: row;
	display: flex;
	margin-top: 25upx;
	padding: 5upx 35upx;
}
.menu-wrapper button{
	width: 160upx;
/* 	height: 85upx; */
	/* border-radius: 0; */
}

.search-box {
    display: flex;
	flex-direction: row;
	padding: 0upx 15upx;
}
.search-input {
   flex-grow: 11;
   height: 75upx;
   line-height: 80upx;
}
.search-btn {
    flex: 1;
	border: none;	
	padding: 10upx 10upx 5upx 10upx;
}
.btn-icon{
	width: 60upx;
	height: 60upx;
	line-height: 80upx;
}
.divider{
	border-top: solid 4upx #ADADAD;
	height: 1upx;
}
.space{
	margin-top: 30upx;
}
</style>
